package com.yuanxing.oushangyidali.ui.home

import androidx.recyclerview.widget.GridLayoutManager
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.ToastUtils
import com.youth.banner.indicator.CircleIndicator
import com.yuanxing.oushangyidali.R
import com.yuanxing.oushangyidali.bean.BannerBean
import com.yuanxing.oushangyidali.ui.adapter.ImageAdapter
import com.yuanxing.oushangyidali.ui.adapter.ManageAdapter
import kotlinx.android.synthetic.main.fragment_home.*

/**
 * @auther : hhx
 * @date   :  2021/3/16
 * @des    : 首页功能fragment
 */
class HomeFragment : BaseFragment() {
    private val adapter: ImageAdapter by lazy {
        ImageAdapter(mutableListOf(), context)
    }
    private val menuAdapter: ManageAdapter by lazy {
        ManageAdapter(mutableListOf(), context)
    }
    private val recordAdapter: ManageAdapter by lazy {
        ManageAdapter(mutableListOf(), context)
    }
    private val statisticsAdapter: ManageAdapter by lazy {
        ManageAdapter(mutableListOf(), context)
    }
    private var bannerlist = mutableListOf<BannerBean>()
    private var menulist = mutableListOf<String>()
    private var recordlist = mutableListOf<String>()
    private var statisticslist = mutableListOf<String>()
    override fun getLayoutId(): Int = R.layout.fragment_home
    override fun main() {
        super.main()
        //banner
        setbanner()
        //初始化菜单
        initmenu()
    }

    fun setbanner() {
        for (i in 0..5) {
            bannerlist.add(
                BannerBean(
                    "https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1820020959,1802557271&fm=11&gp=0.jpg",
                    "title",
                    R.drawable.ic_launcher_background
                )
            )
        }
        refresh.setEnableRefresh(false)
        adapter.setDatas(bannerlist)
        banner?.let {
            it.addBannerLifecycleObserver(this)
            it.indicator = CircleIndicator(context)
            it.adapter = adapter
        }


    }

    private fun initmenu() {
        for (i in 1..6) {
            menulist.add("菜单" + i)
        }
        recycler_menu.layoutManager = GridLayoutManager(context, 3)
        recycler_menu.adapter = menuAdapter
        menuAdapter.setNewInstance(menulist)
        menuAdapter.setOnItemClickListener { adapter, view, position ->
            ToastUtils.show(adapter.getItem(position).toString())
        }
        for (i in 1..3) {
            recordlist.add("记录" + i)
        }
        recycler_record.layoutManager = GridLayoutManager(context, 3)
        recycler_record.adapter = recordAdapter
        recordAdapter.setNewInstance(recordlist)
        recordAdapter.setOnItemClickListener { adapter, view, position ->
            ToastUtils.show(adapter.getItem(position).toString())
        }
        for (i in 1..3) {
            statisticslist.add("菜单" + i)
        }
        recycler_statistics.layoutManager = GridLayoutManager(context, 3)
        recycler_statistics.adapter = statisticsAdapter
        statisticsAdapter.setNewInstance(statisticslist)
        statisticsAdapter.setOnItemClickListener { adapter, view, position ->
            ToastUtils.show(adapter.getItem(position).toString())
        }
    }
}